﻿using System;
using System.Configuration;
using System.Net;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class CommodityBrandController : BaseController
    {
        private readonly CommodityBrandBLL _bll = new CommodityBrandBLL();

        #region 列表

        public ActionResult List()
        {
            ViewBag.IsCreate = MagModules.FindAll(t => t
                .Action=="Create" && t
                    .Controller=="CommodityBrand").Count > 0 ? 1 : 0;
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            int order;
            int.TryParse(Request["order[0][column]"], out order);
            string direction = Request["order[0][dir]"];
            var orderByColumns = new[] {"BrandName", "BrandLetterName"};
            string search = Request["search[value]"];
            int pageIndex = (start/length) + 1;

            TableDataViewDto<ViewCommodityBrandDto> theList = _bll.GetPagingList(MagModules, draw, pageIndex, length, SqlSafe(search),
                orderByColumns[order] + " " + direction);

            JsonResult jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;

            return jsonResult;
        }

        #endregion

        #region 创建

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection, CommodityBrand model)
        {
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            string strUploadImage = UploadImage("fileImage", "/CommodityBrand/Logo/", 50, 50);
            if (!string.IsNullOrEmpty(strUploadImage)) model.BrandLogo = strUploadImage;

            model.AddTime = DateTime.Now;

            int result = _bll.Add(model);

            if (result > 0)
                return RedirectToAction("List", "CommodityBrand");

            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 修改

        public ActionResult Update(int id)
        {
            if (id <= 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            CommodityBrand model = _bll.GetModel(id);
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.NotFound);

            ViewBag.theLogo = ConfigurationManager.AppSettings["ImgFileSitePath"] + model.BrandLogo;

            return View(model);
        }

        [HttpPost]
        public ActionResult Update(FormCollection collection, CommodityBrand model)
        {
            if (model == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            string strUploadImage = UploadImage("fileImage", "/CommodityBrand/Logo/", 50, 50);
            if (!string.IsNullOrEmpty(strUploadImage)) model.BrandLogo = strUploadImage;

            bool result = _bll.Update(model);

            if (result)
                return RedirectToAction("List", "CommodityBrand");

            return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
        }

        #endregion

        #region 删除

        [HttpPost]
        public JsonResult Delete()
        {
            string ids = Request["doIds"];
            bool result = _bll.DeleteList(ids);

            return Json(result);
        }

        #endregion

        #region Private Functions 自用函数

        #endregion
    }
}